package com.itheima.web.controller.ums;


import com.itheima.service.ums.MenuService;
import com.itheima.utils.ResultData;
import com.itheima.vo.ums.UmsMenu;
import com.itheima.vo.ums.UmsMenuTree;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("menu")
public class MenuController {

    @DubboReference
    private MenuService menuService;

    /**
     * 添加后台菜单
     * @param umsMenu
     * @return
     */
    @PostMapping("create")
    public ResultData create(@RequestBody UmsMenu umsMenu){
        log.info("UmsMenu:{umsMenu}" ,umsMenu);
        menuService.save(umsMenu);
        return ResultData.ok("操作成功","");
    }

    /**
     * 根据ID删除后台菜单
     * @param id
     * @return
     */
    @PostMapping("delete/{id}")
    public ResultData delete(@PathVariable Integer id){
        log.info("id:{id}" ,id);
        menuService.removeById(id);
        return ResultData.ok("操作成功","");
    }

    /**
     * 分页查询后台菜单
     * @param parentId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("list/{parentId}")
    public ResultData list(@PathVariable Integer parentId ,Integer pageNum,Integer pageSize){
        return menuService.listParentId(parentId,pageNum,pageSize);
    }


    /**
     * 树形结构返回所有菜单列表
     * @return
     */
    @GetMapping("treeList")
    public ResultData treeList() {
        List<UmsMenuTree> umsMenuTrees = menuService.treeList();
        return ResultData.ok(" ", umsMenuTrees);
    }



    /**
     *修改后台菜单
     * @param id
     * @param umsMenu
     * @return
     */
    @PostMapping("update/{id}")
    public ResultData updateById(@PathVariable Integer id, @RequestBody UmsMenu umsMenu){
        //umsMenu.setId(id);
        menuService.updateById(umsMenu);
        return ResultData.ok("操作成功","");
    }

    /**
     * 修改菜单显示状态
     *
     * @param id
     * @param hidden
     * @return
     */
    @PostMapping("updateHidden/{id}")
    public ResultData updateHidden(@PathVariable Long id, @RequestParam(required = false) Integer hidden) {
        log.info(id + "and" + hidden);
        menuService.updateHidden(id, hidden);
        return ResultData.ok("",null);
    }

    /**
     *根据ID获取菜单详情
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResultData getById(@PathVariable Integer id){
        return ResultData.ok("操作成功",menuService.getById(id));
    }
}
